home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / fp / ifp_unix.lzh / ifp / interp / outob.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-05-23  |  5.8 KB  |  257 lines

  1.  
  2. /****** out.c *********************************************************/
  3. /**                                                                  **/
  4. /**                    University of Illinois                        **/
  5. /**                                                                  **/
  6. /**                Department of Computer Science                    **/
  7. /**                                                                  **/
  8. /**   Tool: IFP                         Version: 0.5                 **/
  9. /**                                                                  **/
  10. /**   Author:  Arch D. Robison          Date:   May 1, 1985          **/
  11. /**                                                                  **/
  12. /**   Revised by: Arch D. Robison       Date:   Feb 8, 1987          **/
  13. /**                                                                  **/
  14. /**   Principal Investigators: Prof. R. H. Campbell                  **/
  15. /**                            Prof. W. J. Kubitz                    **/
  16. /**                                                                  **/
  17. /**                                                                  **/
  18. /**------------------------------------------------------------------**/
  19. /**   (C) Copyright 1987  University of Illinois Board of Trustees   **/
  20. /**                       All Rights Reserved.                       **/
  21. /**********************************************************************/
  22.  
  23.  
  24. #include <stdio.h>
  25. #include <ctype.h>
  26. #include "struct.h"
  27. #include "string.h"
  28.  
  29. #define BerkMode 0
  30.  
  31. #define INDENT 3
  32.  
  33. /*
  34.  * OutIndent
  35.  *
  36.  * Indent N places
  37.  */
  38. void OutIndent (N)
  39.    int N;
  40.    {
  41.       for (; N >= 8; N-=8) printf ("\t");
  42.       while (--N >=0) printf (" ");
  43.    }
  44.  
  45. /*
  46.  * QuoteCheck
  47.  *
  48.  * Check if string should be quoted.
  49.  *
  50.  * Input
  51.  *      S = string
  52.  * Output
  53.  *      result = quote character ('\0','\'', or '\"');
  54.  */
  55. char QuoteCheck (S)
  56.    StrPtr S;
  57.    {
  58.       CharPtr U;
  59.       char Buf[256];
  60.       boolean Single=0,Double=0,Quote=0;
  61.       register char *T;
  62.  
  63.       if (S==NULL) return ('\"');
  64.       else {
  65.      CPInit (&U,&S);
  66.      if (CPRead (&U,Buf,sizeof (Buf))) {
  67.         if (Buf [1] == '\0' && (Buf[0]=='f' || Buf[0]=='t' || Buf[0]=='?'))
  68.            return '\"';
  69.         do
  70.            for (T = Buf; *T; T++)
  71.           if (!isalpha (*T)) {
  72.              Quote=1;
  73.              if (*T == '\'') Single = 1;
  74.              if (*T == '\"') Double = 1;
  75.           }
  76.         while (CPRead (&U,Buf,sizeof (Buf)));
  77.      }
  78.  
  79.      if (!Quote) return '\0';
  80.      else if (Single) return '\"';
  81.      else if (Double) return '\'';
  82.      else return '\"';             /* Should be something else */
  83.       }
  84.    }
  85.  
  86. /*
  87.  * OutString
  88.  *
  89.  * Output a string.
  90.  */
  91. void OutString (S)
  92.    StrPtr S;
  93.    {
  94.       char Buf[256];
  95.       CharPtr U;
  96.  
  97.       if ((Debug & DebugRef) && S != NULL) printf ("[%d]",S->SRef);
  98.       CPInit (&U,&S);
  99.       while (CPRead (&U,Buf,sizeof (Buf))) printf ("%s",Buf);
  100.    }
  101.  
  102. /*
  103.  * OutList
  104.  *
  105.  * Input
  106.  *      P = list to output
  107.  */
  108. void OutList (P)
  109.    register ListPtr P;
  110.    {
  111.       printf ("<");
  112.       if (P!=NIL)
  113.      while (1) {
  114.         if (Debug & DebugRef) printf ("{%d}",P->LRef + (1 - LRefOne));
  115.         OutObject (& P->Val);
  116.         if ((P=P->Next) == NULL) break; 
  117.         else printf (",");
  118.      }
  119.       printf (">");
  120.    }
  121.  
  122.  
  123. /*
  124.  * OutObject
  125.  *
  126.  * Output an object
  127.  *
  128.  * No reference counts change.
  129.  */
  130. void OutObject (X)
  131.    ObjectPtr X;
  132.    {
  133.       if (SysStop > 1) return;
  134.       else if (X == NULL) printf ("(NULL)");
  135.       else
  136.      switch (X->Tag) {
  137.         case BOTTOM: printf ("?"); break;
  138.         case BOOLEAN:
  139.            switch (X->Bool) {
  140.           case 0: printf (BerkMode ? "F" : "f"); break;
  141.           case 1: printf (BerkMode ? "T" : "t"); break;
  142.           default: printf ("(BOOLEAN %d)",X->Bool); break;
  143.            }
  144.            break;
  145.         case INT:
  146.            printf ("%ld",X->Int);
  147.            break;
  148.  
  149.         case FLOAT:
  150.            printf ("%g",X->Float);
  151.            break;
  152.  
  153.         case LIST:
  154.            OutList (X->List);
  155.            break;
  156.  
  157.         case STRING: {
  158.            register char Q;
  159.            Q = QuoteCheck (X->String);
  160.            if (Q) printf ("%c",Q);
  161.            OutString (X->String);
  162.            if (Q) printf ("%c",Q);
  163.         }  break;
  164.  
  165.         case NODE:
  166.            OutNode (X->Node);
  167.            break;
  168.         default:
  169.            printf ("(tag = %d)",X->Tag);
  170.            break;
  171.      }
  172.    }
  173.  
  174. #define LineLength 80
  175.  
  176. /*
  177.  * OutLength
  178.  *
  179.  * Compute approximate number of characters required to output an object.
  180.  * The count is stopped prematurely if it goes over LineLength.
  181.  * 
  182.  * No reference counts change.
  183.  */
  184. private int OutLength (X,Limit)
  185.    ObjectPtr X;
  186.    int Limit;
  187.    {
  188.       register ListPtr P;
  189.       register int K;
  190.  
  191.       if (X == NULL) K = 6;     /* "(null)" */
  192.       else
  193.      switch (X->Tag) {
  194.  
  195.         case BOTTOM:
  196.         case BOOLEAN:
  197.            K = 1; /* "?","t","f" */
  198.            break;
  199.  
  200.         case INT:
  201.            K = 5;
  202.            break;
  203.  
  204.         case FLOAT:
  205.            K = 8;
  206.            break;
  207.  
  208.         case LIST:
  209.            K = 2;                                   /* <> */
  210.            for (P=X->List; P!=NULL && K <= Limit; P=P->Next) 
  211.           K += 1 + OutLength (&P->Val,Limit); /* 1 for space between */
  212.            break;
  213.  
  214.         case STRING:
  215.            K = 2 + LenStr (X->String);  /* "'...'" */
  216.            break;
  217.         default:
  218.            K=0;
  219.            break;
  220.      }
  221.       return K;
  222.    }
  223.  
  224. /*
  225.  * OutPretty
  226.  *
  227.  * Output an object with indented sublists
  228.  *
  229.  * No reference counts change.
  230.  */
  231. void OutPretty (X,Indent)
  232.    ObjectPtr X;
  233.    int Indent;
  234.    {
  235.       register ListPtr P;
  236.  
  237.       if (SysStop > 1) return;
  238.       OutIndent (Indent);
  239.       if (X == NULL) printf ("(null)");
  240.       else if (X->Tag != LIST) OutObject (X);
  241.       else {
  242.      if ((OutLength (X,LineLength) + Indent) > LineLength) {
  243.         printf ("<\n");
  244.         for (P = X->List; P!=NULL; P=P->Next)
  245.            OutPretty (&P->Val,Indent+INDENT);
  246.         OutIndent (Indent);
  247.         printf (">\n");
  248.         return;
  249.      } else OutList (X->List);
  250.       }
  251.       printf ("\n");
  252.    }
  253.  
  254.  
  255. /************************** end of outob.c **************************/
  256.  
  257.